﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace tsgl
{
    public partial class penaltyManage : Form
    {
        public penaltyManage()
        {
            InitializeComponent();
        }

        public string penaltyOperator;
        public float penaltyType;
        public Boolean flagSelect = false;
        public string borrowerId, bibliothecaId;
        public float penaltyMoney, foregift, fineDay;
        public DateTime returnDate;

        public void getBorrowerInfo()
        {
            if (this.txtBorrowerId.Text.Trim() != "")
            {
                DataTable dt;
                borrowerId = txtBorrowerId.Text.Trim();
                dt = loanClass.getBorrowerInfo2(borrowerId);
                if (dt.Rows.Count != 0)
                {
                    txtBorrowerName.Text = dt.Rows[0]["姓名"].ToString();
                    txtForegift.Text = dt.Rows[0]["押金剩余"].ToString();
                    fineDay = float.Parse(dt.Rows[0]["超期日罚金"].ToString());
                    foregift = float.Parse(txtForegift.Text);
                }
            }
        }

        private void setPenaltyData()
        {
            if (this.txtBorrowerId.Text != "")
            {
                switch (tabControl1.SelectedIndex)
                {
                    case 0:
                        DataTable dtOverdue = penaltyClass.getPenaltyBaseInfo("超期归还");
                        cboCaseOverdue.DataSource = dtOverdue;
                        cboCaseOverdue.DisplayMember = "罚款原因";
                        getOverduePenaltyInfo();
                        setLoanOverdue();
                        break;
                    case 1:
                        DataTable dtMar = penaltyClass.getPenaltyBaseInfo("图书损坏");
                        cboCaseMar.DataSource = dtMar;
                        cboCaseMar.DisplayMember = "罚款原因";
                        getMarPenaltyInfo();
                        setLoanMar();
                        break;
                    case 2:
                        DataTable dtLose = penaltyClass.getPenaltyBaseInfo("图书丢失");
                        cboCaseLose.DataSource = dtLose;
                        cboCaseLose.DisplayMember = "罚款原因";
                        getLosePenaltyInfo();
                        setLoanLose();
                        break;
                }
            }
        }

        public static DataTable getOverdueInfo(string borrowerId)
        {
            string str;
            if (borrowerId != "")
            {
                str = "select 借阅者编号,姓名,图书条码,价格,借出日期,应还日期,续借次数,图书借阅员,书目编号 form loanView where 应还日期 < '" + DateTime.Now.Date.ToShortDateString() + "' and 借阅者编号 = '" + borrowerId + "'";
                MessageBox.Show(borrowerId);
            }
            else
            {
                str = "select 借阅者编号,姓名,图书条码,价格,借出日期,应还日期,续借次数,图书借阅员,书目编号 form loanView where 应还日期 < '" + DateTime.Now.Date.ToShortDateString() + "'";
            }
            MessageBox.Show(str);
            return bookDbClass.ExecuteDataTable(str);
        }
        
        private void getOverduePenaltyInfo()
        {
            DataTable dt = getOverdueInfo(this.txtBorrowerId.Text.Trim());
            if (dt.Rows.Count != 0)
            {
                this.dataGridView1.DataSource = dt.DefaultView;
            }
            else
            {
                dataGridView1.DataSource = null;
            }
        }

        private void setLoanOverdue()
        {
            if (this.txtBorrowerId.Text.Trim() != "")
            {
                this.txtBookCode.Text = this.dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                this.txtBookName.Text = this.dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                this.txtBookPrice.Text = this.dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                returnDate = DateTime.Parse(this.dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString());
                bibliothecaId = this.dataGridView1[9, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtBaseOverdue.Text =fineDay.ToString();
                lblOverdueDay.Text = returnDate.ToString();
            }
        }

        private void getMarPenaltyInfo()
        {
            DataTable dt = loanClass.getLoanInfo(txtBorrowerId.Text.Trim());
            if (dt.Rows.Count != 0)
            {
                dataGridView2.DataSource = dt.DefaultView;

            }
            else
            {
                dataGridView2.DataSource = null;
            }
        }

        private void setLoanMar()
        {
            if (txtBorrowerId.Text.Trim() != "")
            {
                if (dataGridView2.DataSource != null)
                {
                    txtBookCode.Text = dataGridView2[2, dataGridView2.CurrentCell.RowIndex].Value.ToString();
                    txtBookName.Text = dataGridView2[4, dataGridView2.CurrentCell.RowIndex].Value.ToString();
                    txtBookPrice.Text = dataGridView2[5, dataGridView2.CurrentCell.RowIndex].Value.ToString();
                    txtBaseMar.Text = dataGridView2[5, dataGridView2.CurrentCell.RowIndex].Value.ToString();
                    bibliothecaId = dataGridView2[3, dataGridView2.CurrentCell.RowIndex].Value.ToString();
                }
            }
        }

        private void getLosePenaltyInfo()
        {
            DataTable dt = loanClass.getBookInfo(txtBorrowerId.Text.Trim());
            if (dt.Rows.Count != 0)
            {
                dataGridView3.DataSource = dt.DefaultView;

            }
            else
            {
                dataGridView3.DataSource = null;
            }
        }

        private void setLoanLose()
        {
            if (txtBorrowerId.Text.Trim() != "")
            {
                if (dataGridView3.DataSource != null)
                {
                    txtBookCode.Text = dataGridView3[2, dataGridView3.CurrentCell.RowIndex].Value.ToString();
                    txtBookName.Text = dataGridView3[4, dataGridView3.CurrentCell.RowIndex].Value.ToString();
                    txtBookPrice.Text = dataGridView3[5, dataGridView3.CurrentCell.RowIndex].Value.ToString();
                    txtBaseLose.Text = dataGridView3[5, dataGridView3.CurrentCell.RowIndex].Value.ToString();
                    bibliothecaId = dataGridView3[3, dataGridView3.CurrentCell.RowIndex].Value.ToString();
                }
            }
        }

        private void setControlInvalid()
        {
            txtBookName.Enabled = false;
            txtForegift.Enabled = false;
            txtBaseOverdue.Enabled = false;
            txtMultipleOverdue.Enabled = false;
            txtBaseMar.Enabled = false;
            txtMultipleMar.Enabled = false;
            txtBaseLose.Enabled = false;
            txtMultipleLose.Enabled = false;
            txtBookCode.Enabled = false;
            txtBookName.Enabled = false;
            txtBookPrice.Enabled = false;
            cboCaseOverdue.Enabled = false;
            cboCaseMar.Enabled = false;
            cboCaseLose.Enabled = false;
            cboPenaltyWay.Enabled = false;
        }

        private void setControlValid()
        {
            cboCaseOverdue.Enabled = true;
            cboCaseMar.Enabled = true;
            cboCaseLose.Enabled = true;
            cboPenaltyWay.Enabled = true;
        }

        private void clearControl()
        {
            txtBookName.Enabled = false;
            txtForegift.Enabled = false;
            txtBaseOverdue.Enabled = false;
            txtMultipleOverdue.Enabled = false;
            txtBaseMar.Enabled = false;
            txtMultipleMar.Enabled = false;
            txtBaseLose.Enabled = false;
            txtMultipleLose.Enabled = false;
            txtBookCode.Enabled = false;
            txtBookName.Enabled = false;
            txtBookPrice.Enabled = false;
        }

        public void penaltyRecordAdd(string borrwoerId, string borrowerName, string bookCode, string bookName, string penaltyCause, string penaltyWay, float penaltyMoney, DateTime penaltyDate, string operatorName, string tableName)
        {
            string strInsertComm;
            if (tableName == "罚款信息")
            {
                try
                {
                    strInsertComm = "insert into 罚款信息(借阅者编号,借阅者姓名,图书条码,图书名称,罚款原因,罚款方法,罚款金额,罚款日期,操作员) values(@a,@b,@c,@d,@e,@f,@g,@h,@i)";
                    //strInsertComm = "insert into 罚款信息(借阅者编号,借阅者姓名,图书条码,图书名称,罚款原因,罚款方法,罚款金额,罚款日期,操作员) values('"+borrwoerId+"','"+borrowerName+"','"+bookCode+"','"+bookName+"','"+penaltyCause+"','"+penaltyWay+"','"+penaltyMoney+"','"+penaltyDate+"','"+operatorName+"','"+tableName+"')";
                    bookDbClass.ExecuteDataTable(strInsertComm, new SqlParameter("a", borrwoerId), new SqlParameter("b", borrowerName), new SqlParameter("c", bookCode), new SqlParameter("d", bookName), new SqlParameter("e", penaltyCause), new SqlParameter("f", penaltyWay), new SqlParameter("g", penaltyDate), new SqlParameter("h", operatorName), new SqlParameter("i", tableName));
                    //return bookDbClass.ExecuteDataTable(strInsertComm);
                    //return true;
                }
                catch (Exception)
                {
                    //return false;
                }
            }
            if (tableName == "待罚款信息")
            {
                try
                {
                    strInsertComm = "insert into 待罚款信息(借阅者编号,借阅者姓名,图书条码,图书名称,罚款原因,罚款方法,罚款金额,罚款日期,操作员) values(@a,@b,@c,@d,@e,@f,@g,@h,@i)";
                    //strInsertComm = "insert into 罚款信息(借阅者编号,借阅者姓名,图书条码,图书名称,罚款原因,罚款方法,罚款金额,罚款日期,操作员) values('" + borrwoerId + "','" + borrowerName + "','" + bookCode + "','" + bookName + "','" + penaltyCause + "','" + penaltyWay + "','" + penaltyMoney + "','" + penaltyDate + "','" + operatorName + "','" + tableName + "')";
                    bookDbClass.ExecuteDataTable(strInsertComm, new SqlParameter("a", borrwoerId), new SqlParameter("b", borrowerName), new SqlParameter("c", bookCode), new SqlParameter("d", bookName), new SqlParameter("e", penaltyCause), new SqlParameter("f", penaltyWay), new SqlParameter("g", penaltyDate), new SqlParameter("h", operatorName), new SqlParameter("i", tableName));
                    //return bookDbClass.ExecuteDataTable(strInsertComm);
                    //return true;
                }
                catch (Exception)
                {
                    //return false;
                }
            }
        }

        string penaltyReson1;
        private Boolean penalty(string strTableName)
        {
            if (txtBorrowerId.Text == "")
            {
                MessageBox.Show("请确定借阅者", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (penaltyMoney > foregift || cboPenaltyWay.SelectedIndex == 1)
            {
                MessageBox.Show("押金不够，请教现款！", "提示信息");
                cboPenaltyWay.SelectedIndex = 0;
            }
            try
            {

                if (txtBookCode.Text.Trim() != "")
                {
                    string borrowerName, bookCode, bookName;
                    string penaltyWay;
                    DateTime penaltyDate;
                    borrowerId = txtBorrowerId.Text.Trim();
                    borrowerName = txtBorrowerName.Text.Trim();
                    bookCode = txtBookCode.Text.Trim();
                    bookName = txtBookName.Text.Trim();
                    switch (tabControl1.SelectedIndex)
                    {
                        case 0:
                          penaltyReson1 = cboCaseOverdue.Text.Trim();
                            break;
                        case 1:
                           penaltyReson1 = cboCaseMar.Text.Trim();
                            break;
                        case 2:
                           penaltyReson1 = cboCaseLose.Text.Trim();
                            break;
                    }
                   
                    penaltyWay = cboPenaltyWay.Text.Trim();
                    penaltyOperator = "admin";
                    penaltyDate =DateTime.Parse(DateTime.Now.Date.ToShortDateString());
                    //if (penaltyClass.penaltyRecordAdd(borrowerId, borrowerName, bookCode, bookName, penaltyReson, penaltyWay, penaltyMoney, penaltyDate, penaltyOperator, strTableName) == true)
                    try
                    {
                        penaltyRecordAdd(borrowerId, borrowerName, bookCode, bookName, penaltyReson1, penaltyWay, penaltyMoney, penaltyDate, penaltyOperator, strTableName);
                        //{
                            if (loanClass.loanDelete(borrowerId, bookCode) == true)
                            {
                                if (tabControl1.SelectedIndex != 2)
                                {
                                    loanClass.setBookState(bookCode, "在藏");
                                    loanClass.bookNowNumAdd(bibliothecaId);
                                }
                                else
                                {
                                    loanClass.setBookState(bookCode, "丢失");
                                    loanClass.bookTotalNumReduce(bibliothecaId);
                                    loanClass.bookNowNumReduce(bibliothecaId);
                                }
                                if (strTableName == "罚款信息")
                                {
                                    if (cboPenaltyWay.Text.Trim() == "从押金中扣除")
                                    {
                                        if (penaltyMoney <= foregift)
                                        {
                                            penaltyClass.foreigftReduce(borrowerId, penaltyMoney);
                                        }
                                    }
                                }
                            }
                        //}
                    }
                    catch(Exception)
                    {
                        MessageBox.Show("失败");
                    }
                }
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show("罚款操作失败，其原因是：" + ex.Message, "提示信息");
                return false;
            }
            return true;
        }

        private void penaltyManage_Load(object sender, EventArgs e)
        {
            cboPenaltyWay.Items.Add("交现款");
            cboPenaltyWay.Items.Add("从押金中扣除");
            cboPenaltyWay.SelectedIndex = 0;
            tabControl1.SelectedIndex =int.Parse(penaltyType.ToString());
            setControlInvalid();
            btnPenalty.Enabled = false;
            if(txtBorrowerId.Text!="")
            {
                getBorrowerInfo();
                setPenaltyData();
                setControlInvalid();
            }
            else
            {
                txtBorrowerId.Focus();
            }
        }

        private void btnSelectBorrwer_Click(object sender, EventArgs e)
        {
            selectBorrower frm = new selectBorrower();
            frm.ShowDialog();
            this.txtBorrowerId.Text = frm.getBorrowerId();
        }

        private void txtBorrowerId_TextChanged(object sender, EventArgs e)
        {
            if (flagSelect == true)
            {
                if (txtBorrowerId.Text.Trim() != "")
                {
                    getBorrowerInfo();
                    setPenaltyData();
                    setControlInvalid();
                }
                else
                {
                    setControlInvalid();
                    clearControl();
                }
                flagSelect = false;
            }
        }

        private void txtBorrowerId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtBorrowerId.Text.Trim() != "")
                {
                    getBorrowerInfo();
                    setPenaltyData();
                    setControlInvalid();
                }
                else
                {
                    setControlInvalid();
                    clearControl();
                }
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            setPenaltyData();
        }

        private void tabControl1_Click(object sender, EventArgs e)
        {
            if (txtBorrowerId.Text == "")
            {
                MessageBox.Show("请先输入或选择借阅者编号","提示信息");
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            setLoanOverdue();
        }

        private void dataGridView2_Click(object sender, EventArgs e)
        {
            setLoanMar();
        }

        private void dataGridView3_Click(object sender, EventArgs e)
        {
            setLoanLose();
        }

    }
}